<?php

namespace app\common\model\Product;

// 引入TP基础模型
use think\Model;

//  订单模型
class  Order extends Model
{
    protected $name = 'order';

    protected $autoWriteTimestamp = true;

    protected $createTime = 'createtime';

    protected $updateTime = false;


    protected $append = [
        'status_text', // 状态
        'express_text', // 物流信息
        'back_text', // 售后状态
    ];

    public function getStatusTextAttr($value, $data)
    {
        $status = !empty($data['status'])  ? $data['status'] : '';

        if ($status) {
            switch ($status) {
                case '1':
                    return '已支付';
                case '2':
                    return '已发货';
                case '3':
                    return '已收货';
                case '4':
                    return '已完成';
                case '-1':
                    return '仅退款';
                case '-2':
                    return '退款退货';
                case '-3':
                    return '待退货';
                case '-4':
                    return '退款成功';
                case '-5':
                    return '退款失败';
            }
        }
    }

    public function getExpressTextAttr($value, $data)
    {
        $expressid = !empty($data['expressid']) ? $data['expressid'] : '';

        $name = '';

        if (!empty($expressid)) {
            $name = model('Expressquery')::where('id', $expressid)->value('name');
        }

        return $name;
    }

    public function getBackTextAttr($value, $data)
    {
        $back = model('Depot.Back')::where('ordercode', $data['code'])->find();

        if($back)
        {
            return true;
        }
        else
        {
            return false;
        }
    }

    public function address()
    {
        return $this->belongsTo('app\common\model\Business\Address', 'businessaddrid', 'id', [], 'LEFT')->setEagerlyType(0);
    }
}